home *** CD-ROM | disk | FTP | other *** search
/ F1 Licenseware / F1 Licenseware - Volume 1.iso / disks / 050a.dms / 050a.adf / TEXTS / chapter04.txt < prev    next >
Text File  |  1992-02-26  |  6KB  |  131 lines

  1.                      The Absolute Beginners Guide To Amos
  2.                     -------------------------------------
  3.                                  Chapter Four
  4.                                 -------------
  5. Now I think it is time to tackle our first real program, a times table
  6. calculator.  Our program will ask the user to type in any number from
  7. 1 to 99 and will then print the times table for that number.
  8.  
  9. Sounds complicated doesn`t it? How many lines do you think all those
  10. possibilities of every table from 1 to 99 will take up? Not many is the
  11. answer, onward >
  12.  
  13. This time, before I go explaining every command I will show you the main
  14. part of our Times Table Calculator:
  15.  
  16. LINE INPUT "Please type in a number from 1-99 ";tt
  17. CLS 0: CURS OFF
  18. FOR A=1 TO 12
  19. PRINT A*TT
  20. NEXT A
  21.  
  22. This, believe it or not, is most of the program, I will now break down each 
  23. command:
  24.  
  25. LINE INPUT "Please type a number from 1-99 ";tt
  26.  
  27. This strange beast allows the user to pass a value typed in from the 
  28. keyboard to our program, the format of LINE INPUT is usually,
  29.  
  30. LINE INPUT "SOME TEXT IN QUOTES ";variable
  31. ------------------------------------------
  32. In our case we want the text to ask the user to type in a number from 1-99,
  33. of course you can re-word the text in any way you wish. Note the space 
  34. after the text? This is to keep it neat, you will see what I mean if you 
  35. delete the space from this line in EXAMPLE4.Amos, but don`t forget the quotes
  36. around the text just as we do with the PRINT statement. 
  37. The quote is then followed by a semi-colon which tells Amos to expect a 
  38. variable (remember them?) Our variable which I have called TT will hold the
  39. users input. I could of called TT anything, Amos legal, of course. 
  40. Now Amos knows which times table it has to calculate and PRINT to the screen.
  41.  
  42. CLS 0: CURS OFF
  43. ---------------
  44. You should know these commands, check your notes.
  45.  
  46. FOR A=1 TO 12
  47. -------------
  48. Wow, another weird looking command! This is called a FOR NEXT loop and you 
  49. won`t find many programs in Basic that don`t make use of FOR NEXT loops. 
  50. They are very powerful and very useful.
  51.  
  52. A FOR NEXT loop does all the hard work for you. What we are doing is telling
  53. Amos to loop twelve times between the FOR command and the NEXT command
  54. and execute everything in between:
  55.  
  56. FOR A=1 TO 12       REM Start of loop
  57.  
  58. do something here   REM Execute some commands
  59.  
  60. NEXT A              REM If A is less than 12 INC A and jump backward and
  61.                     REM execute the command(s) in between. If A=12 then
  62.                     REM go to the line after NEXT A and continue 
  63.                     REM executing the rest of the program.
  64.  
  65. Here is an example use of a FOR NEXT loop that I could of used in
  66. Example1.Amos to display CLS 0 to CLS 15:
  67.  
  68. FOR B=0 TO 15       REM The loop starts at 0 and ends at 15
  69. CLS B               REM Each pass of the loop will CLear the Screen in the
  70.                     REM colour of the loop counter (b), so if B equalled 0
  71.                         like it would on the very first loop the program 
  72.                         would clear the screen to black. The same as CLS 0
  73. WAIT KEY            REM WAIT for the user to press a KEY, giving the user
  74.                         a chance to see each colour, otherwise it would be
  75.                         over in a flash!
  76. NEXT B              REM When the program reaches here Amos looks at the
  77.                         value of the loop counter, in this case B to check
  78.                         if it is equal to the maximum loopage, 15 in this
  79.                         case. If the loop counter is less than 15 it will
  80.                         INC B and loop back to the CLS command. When B 
  81.                         eventually reaches 15 the loop will end and Amos
  82.                         will skip past the NEXT B instruction and execute
  83.                         the following line. If there is no line then the
  84.                         program will end.
  85.  
  86. Don't forget that the loop counter variable can be any legal letters you
  87. choose just like any other variable:
  88.  
  89. FOR ZASQ=1 TO 60000     FOR T=-8 TO -2    FOR QQ=7 TO 9  FOR LCOUNT=1 TO 6
  90.  
  91. Etc. Etc.
  92.  
  93. This is a complex concept to explain and is better seen in action. 
  94. FOR NEXT is explained further in EXAMPLE4.Amos. Make sure you change the 
  95. values of A to see it`s effects. 
  96.  
  97. Moving on to the next line in our Times Table program we have:
  98.  
  99. PRINT A*TT
  100. ----------
  101. We know about the PRINT command e.g. PRINT "Testing..." But this PRINT is 
  102. slightly different.  Amos is doing two jobs here, it`s calculating A*TT in
  103. which A is a number starting from 1 and ending at 12 and TT is the users
  104. input number, so let`s presume the user typed in 10 as his/her selected Times
  105. Table and this was the first loop of the FOR NEXT loop
  106.  
  107. A  would =1       A is the loop counter variable
  108. TT would =10     TT is the number the user typed in and the times table we
  109.                     we have to display.  
  110.  
  111. so A*TT would =10
  112.  
  113. which is the same as saying PRINT "10", we do not need quotes when printing 
  114. a variable, in fact you shouldn`t put quotes as this is what would 
  115. happen,
  116.  
  117. PRINT "A*TT"
  118. ------------
  119. On screen you would get the result of,
  120.  
  121. A*TT
  122.  
  123. instead of 10, why? Because Amos thinks that A*TT is a word you want printed
  124. and not a calculation you want printed as it was wrapped in quotes.
  125.  
  126. That`s it for this chapter, you will need to load EXAMPLE4.Amos for more
  127. advice and explanations. Believe me it`s a lot easier to understand that way.
  128.  
  129.                              End of chapter four
  130.                              ^^^^^^^^^^^^^^^^^^^
  131.